package game.player.pacman;

import game.core.G;
import game.core.Game;
import gui.AbstractPlayer;
/**
 * Targets for Ms PacMan are specified in the method getAction.
 * 
 * TODO: What is the return-statement of getAction really doing?
 *       Implementing intelligent flee-mod for Ms PacMan
 * @author Daniel Dollinger
 *
 */
public class PacDaniel extends AbstractPlayer{
	private long lastDecision = 0;
	private boolean flee = false;
	@Override
	public int getAction(Game game, long timeDue) 
	{
		for(int i=0; i< Game.NUM_GHOSTS;i++)
		{
			if(game.getPathDistance(game.getCurPacManLoc(),game.getCurGhostLoc(i)) < 10)
			{
				flee = true;
			}
			else
				flee = false;
			
			if(flee){ // Weglaufen
				int nextGhost = 0;
				int distanceToGhost = Integer.MAX_VALUE;
				for(int n=0; n< Game.NUM_GHOSTS;n++)
				{
					if(game.getPathDistance(game.getCurPacManLoc(),game.getCurGhostLoc(n)) < distanceToGhost)
					{
						
						distanceToGhost = game.getPathDistance(game.getCurPacManLoc(),game.getCurGhostLoc(n));
						nextGhost = n;
					}
				}
				int[] neighbours = game.getPacManNeighbours();
				int newDistance = -1;
				int dir = 4; // neutral
				for(int m=0; m<neighbours.length;m++)
				{
					if(neighbours[m]<0)
					{ // Falls keine Zelle in dieser Nachbarschaft ist
						continue;
					}
					int tmpDistance = game.getPathDistance(neighbours[m], game.getCurGhostLoc(nextGhost));
					if(tmpDistance > newDistance)
					{
						dir = m;
					}
				}
				return dir;
			}
			else
			{ // Pills sammeln 
				int current=game.getCurPacManLoc();
				System.out.println(current);
				//alle aktiven pills
				int[] activePills=game.getPillIndicesActive();
				//get all aktiven power pills
				int[] activePowerPills=game.getPowerPillIndicesActive();
				//alle möglichen Ziele (pill + powerPills) in ziele kopieren
				int[] targetsArray=new int[activePills.length+activePowerPills.length];
				for(int t=0;i<activePills.length;t++)
				{
					targetsArray[t]=activePills[t];
					
				}
				
				for(int t=0;t<activePowerPills.length;t++)
				{
					targetsArray[activePills.length+t]=activePowerPills[t];	
				}
				// laeuft zu den neachsten zielen
				return game.getNextPacManDir(game.getTarget(current,targetsArray,true,G.DM.PATH),true,Game.DM.PATH);
			}
		}
		return 0;
	}





	@Override
	public String getGroupName() 
	{
		// TODO Auto-generated method stub
		return "PacDaniel";
	}

}
